package com.vd.tbsms.process.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vd.tbsms.parse.BeanToXmlParse;

/**
 * 邮件报文生成类
 * @author lizhenhua
 *
 */
public class GenMailProcess {
	private static final Log log = LogFactory.getLog(GenMailProcess.class);
	
	/**
	 * 生成邮件报文
	 * @param wageMap
	 * @param content
	 * @param isContainTable
	 * @return 邮件报文
	 */
	public static String genMailInfo(Map<String, String> wageMap,String content,boolean isContainTable){
		// 获取内容中所有需做替换的关键字
		List<String> contentKeys = getContentKeys(content);
		
		// 构建邮件文本内容
		String result = genMailContent(wageMap, contentKeys, content);
		
		// 组装完整的邮件报文
		String mailMsg = "";
		if (isContainTable) {
			String tableMsg = genMailTable(wageMap);
			mailMsg = genMailInfo(result, tableMsg);
		} else {
			mailMsg = genMailInfo(result, "");
		}		
		return mailMsg;
	}
		
	/**
	 * 生成邮件报文
	 * 
	 * @param content
	 * @param tableMsg
	 * @return
	 */
	private static String genMailInfo(String content, String tableMsg) {

		BeanToXmlParse parse = BeanToXmlParse.getInstance();
		parse.putArgs("content", content, null);
		parse.putArgs("mailTabMsg", tableMsg, null);
		try {
			String mailMsg = parse.parse("mail.vm");
			return mailMsg;
		} catch (Exception ex) {
			log.error("生成邮件报文失败");
			throw new RuntimeException(ex);
		}
	}
	
	/**
	 * 生成邮件表格部分
	 * 
	 * @param wageMap
	 * @return
	 */
	private static String genMailTable(Map<String, String> wageMap) {
		List<String> wageKeyLst = new ArrayList<String>();
		List<String> wageValueLst = new ArrayList<String>();

		for (String key : wageMap.keySet()) {
			if (!"邮箱地址".equals(key) && !"发送状态".endsWith(key)) {
				log.info("add key:"+key);
				wageKeyLst.add(key);
				wageValueLst.add(wageMap.get(key));
			}
		}

		BeanToXmlParse parse = BeanToXmlParse.getInstance();
		parse.putArgs("wageKeyLst", wageKeyLst, null);
		parse.putArgs("wageValueLst", wageValueLst, null);
		try {
			String tableMsg = parse.parse("mailTab.vm");
			return tableMsg;
		} catch (Exception ex) {
			log.error("生成邮件表格部分失败");
			throw new RuntimeException(ex);
		}
	}
	
	/**
	 * 生成邮件文本内容
	 * @param map
	 * @param keys
	 * @param content
	 * @return
	 */
	private static String genMailContent(Map<String, String> map, List<String> keys,
			String content) {
		for (String key : keys) {
			String value = map.get(key);
			if (value != null) {
				content = content.replace("{" + key + "}", value);
			}
		}
		content = content.replaceAll("\n", "<br/>");
		content = content.replaceAll("\t", "&nbsp;");
		content = content.replaceAll(" ", "&nbsp;");
		return content;
	}
	
	/**
	 * 获取所有需做替换的关键字
	 * 
	 * @param content
	 * @return
	 */
	private static List<String> getContentKeys(String content) {
		List<String> keys = new ArrayList<String>();
		Pattern ptn = Pattern.compile("\\{([^}]+)\\}");
		Matcher mtcher = ptn.matcher(content);
		while (mtcher.find()) {
			keys.add(mtcher.group(1));
		}
		return keys;
	}
}
